#{include file="../Header.html"}

<!--begin::Content-->
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
    <!--begin::Toolbar-->
    #{include file="../Toolbar.html"}
    <!--end::Toolbar-->
    <!--begin::Post-->
    <div class="post d-flex flex-column-fluid" id="kt_post">
        <!--begin::Container-->
        <div id="kt_content_container" class="container-fluid">
            <!--begin::Tables Widget 9-->
            <div class="card mb-5 mb-xl-8">
                <!--begin::Header-->
                <div class="card-header border-0 pt-5">
                    <div class="card-toolbar">
                        <button class="btn btn-sm btn-light-primary me-3 developerCreatePlugin"><i
                                class="far fa-plus-square"></i> 创建插件
                        </button>
                    </div>
                </div>
                <!--end::Header-->

                <div class="card-body py-3">
                    <form class="search-query"></form>
                    <table id="plugin"></table>
                </div>
            </div>

            <!--end::Tables Widget 9-->
        </div>
        <!--end::Container-->
    </div>
    <!--end::Post-->
</div>
<!--end::Content-->

<script>


    $(function () {
        layui.use(['hex', 'form'], function () {
            let table = $("#plugin");
            let pluginLoadIndex;
            let form = layui.form;
            let cao = layui.hex;
            let type = ["<span class='badge badge-light-primary'>通用扩展</span>", "<span class='badge badge-light-success'>支付扩展</span>", "<span class='badge badge-light-info'>网站模板</span>"];
            let status = ["<span class='badge badge-light'>开发中</span>", "<span class='badge badge-light-success'>已上架</span>", "<span class='badge badge-light-danger'>审核不通过</span>", "<span class='badge badge-light-dark'>审核中</span>"];


            let modal = (values = {}) => {
                cao.popup('/admin/api/app/developerCreatePlugin', [
                    {
                        title: "插件图标",
                        name: "icon",
                        type: "image",
                        placeholder: "120*120",
                        width: 60
                    },
                    {
                        title: "插件标识",
                        name: "plugin_key",
                        type: "input",
                        placeholder: "插件唯一标识，仅支持字母，也就是你插件文件夹的名字"
                    },
                    {
                        title: "插件名字",
                        name: "plugin_name",
                        type: "input",
                        placeholder: "插件名称"
                    },

                    {
                        title: "扩展类型",
                        name: "type",
                        type: "select",
                        placeholder: "请选择插件类型",
                        dict: [
                            {id: 0, name: "通用扩展"},
                            {id: 1, name: "支付扩展"},
                            {id: 2, name: "网站模板"},
                        ],
                        default: 0
                    },
                    {
                        title: "免费组",
                        name: "group",
                        type: "select",
                        placeholder: "请选择",
                        dict: [
                            {id: 0, name: "不启用"},
                            {id: 1, name: "专业版/企业版免费使用"},
                            {id: 2, name: "企业版免费使用"},
                        ],
                        default: 0
                    },
                    {
                        title: "版本号",
                        name: "version",
                        type: "input",
                        placeholder: "版本号",
                        default: "1.0.0"
                    },
                    {
                        title: "插件说明",
                        name: "description",
                        type: "textarea",
                        placeholder: "插件说明，60字内"
                    },
                    {
                        title: "插件官网",
                        name: "web_site",
                        type: "input",
                        placeholder: "可以是插件演示地址，或者您的个人博客，如果是非法网站将会被替换成#"
                    },
                    {
                        title: "插件价格",
                        name: "price",
                        type: "input",
                        placeholder: "可忽略不填，自动默认免费"
                    },
                ], res => {
                    table.bootstrapTable('refresh', {silent: true});
                }, values, "660px", false, '<i class="far fa-plus-square"></i> 创建插件');
            }
            let createKit = (values = {}) => {
                cao.popup('/admin/api/app/developerCreateKit', [
                    {
                        title: "审核时间",
                        name: "explain1",
                        type: "explain",
                        placeholder: '<b style="color: green;">更新审核时间为早上10:00~晚上22:00，预计2小时内</b>'
                    },
                    {
                        title: "插件安装包",
                        name: "resource",
                        type: "file",
                        exts: "zip",
                        acceptMime: ".zip",
                        placeholder: "请选择插件安装包(.zip)"
                    },
                    {
                        title: "说明",
                        name: "explain",
                        type: "explain",
                        placeholder: "插件安装包请直接在您插件根目录进行打包，而不是将插件文件夹也一起打包上来，并且仅支持zip打包方式，请勿设置压缩包密码，如果插件带数据库，请将数据库安装SQL命令写到install.sql中(sql文件中不要带注释)，并且放置在插件根目录"
                    },
                ], res => {
                    table.bootstrapTable('refresh', {silent: true});
                }, values, "560px", false, '<i class="fas fa-upload"></i> 上传插件安装包');
            }
            let updatePlugin = (values = {}) => {
                cao.popup('/admin/api/app/developerUpdatePlugin', [
                    {
                        title: "审核时间",
                        name: "explain1",
                        type: "explain",
                        placeholder: '<b style="color: green;">更新审核时间为早上10:00~晚上22:00，预计2小时内，如果是紧急更新，请在更新内容中说明，会加快审核速度</b>'
                    },
                    {
                        title: "插件更新包",
                        name: "audit_resource",
                        type: "file",
                        exts: "zip",
                        acceptMime: ".zip",
                        placeholder: "请选择插件更新包(.zip)"
                    },
                    {
                        title: "说明",
                        name: "explain",
                        type: "explain",
                        placeholder: "更新包说明，如果带有更新数据库的情况下，请仔细编写update.sql放置插件更新包的根目录（请使用SQL命令检测当前更改项是否可以更改再去更改,否则产生错误将使插件更新失败，并且该update.sql应该从最初始版本累计，sql文件中不要带注释），如果是支付扩展或者通用扩展，请一定要删除配置文件Config.php"
                    },
                    {
                        title: "版本号",
                        name: "audit_version",
                        type: "input",
                        placeholder: "这个更新包内Info信息中的版本号，请填写一致"
                    },
                    {
                        title: "更新内容",
                        name: "audit_update_content",
                        type: "textarea",
                        placeholder: "请填写更新内容"
                    },
                ], res => {
                    table.bootstrapTable('refresh', {silent: true});
                }, values, "560px", false, '<i class="fas fa-sync-alt"></i> 上传插件更新包 - <b style="color: #0d8ddc;">' + values.plugin_name + '</b>');
            }

            let pluginPriceSet = (values = {}) => {
                cao.popup('/admin/api/app/developerPluginPriceSet', [
                    {
                        title: "定价说明",
                        name: "explain1",
                        type: "explain",
                        placeholder: '<b style="color: green;">市场定价修改立即生效，如果想设置为免费，填写0元即可</b>'
                    },

                    {
                        title: "市场定价",
                        name: "price",
                        type: "input",
                        placeholder: "市场出售价格，0=免费"
                    }
                ], res => {
                    table.bootstrapTable('refresh', {silent: true});
                }, values, "560px", false, '<i class="fas fa-funnel-dollar"></i> 市场定价 - <b style="color: #0d8ddc;">' + values.plugin_name + '</b>');
            }

            let queryParams = null;
            table.bootstrapTable({
                url: '/admin/api/app/developerPlugins',//请求的url地址
                method: "post",//请求方式
                // striped:true,//是否显示行间隔色
                pageSize: 18,//每页显示的数量
                pageList: [18, 25, 50, 100],
                showRefresh: false,//是否显示刷新按钮
                cache: false,//是否使用缓存
                showToggle: false,//是否显示详细视图和列表视图的切换按钮
                cardView: false,
                pagination: true,//是否显示分页
                pageNumber: 1,//初始化显示第几页，默认第1页
                singleSelect: false,//复选框只能选择一条记录
                sidePagination: 'server',//分页显示方式，可以选择客户端和服务端（server|client）
                contentType: "application/x-www-form-urlencoded",//使用post请求时必须加上
                dataType: "json",//接收的数据类型
                queryParamsType: 'limit',//参数格式，发送标准的Restful类型的请求
                queryParams: function (params) {
                    params.page = (params.offset / params.limit) + 1;
                    if (queryParams) {
                        for (const key in params) {
                            queryParams[key] = params[key];
                        }
                    } else {
                        queryParams = params;
                    }
                    return queryParams;
                },
                //回调函数
                responseHandler: function (res) {
                    return {
                        "total": res.count,
                        "rows": res.data
                    }
                },
                columns: [

                    {
                        field: 'plugin_name', title: '插件名称', formatter: function (val, item) {
                            return '<span class="badge badge-light-dark"><img src="#{App\Service\App::APP_URL}' + item.icon + '"  style="width: 18px;border-radius: 5px;margin-top: -2px"/> ' + item.plugin_name + '</span> '
                        }
                    }
                    ,
                    {
                        field: 'type', title: '插件类型', formatter: function (val, item) {
                            return type[item.type];
                        }
                    }
                    ,
                    {
                        field: 'description', title: '说明', formatter: function (val, item) {
                            return '<span class="badge badge-light">' + item.description + '</span>';
                        }
                    },
                    {
                        field: 'description', title: '官网/演示地址', formatter: function (val, item) {
                            return '<a href="' + item.web_site + '" target="_blank"><span class="badge badge-light-primary">' + item.web_site + '</span></a>';
                        }
                    },
                    {
                        field: 'version', title: '线上版本', formatter: function (val, item) {
                            return '<span class="badge badge-light">' + item.version + '</span>';
                        }
                    },
                    {
                        field: 'price', title: '市场售价', formatter: function (val, item) {
                            if (item.price == 0) {
                                return "<span class='badge badge-light-success'>免费</span>";
                            }
                            let html = " <span class='badge badge-light-danger'>" + item.price + "</span> ";
                            if (item.group == 1) {
                                html += " <span class='badge badge-light-primary'>专业版免费</span> <span class='badge badge-light-success'>企业版免费</span> ";
                            }
                            if (item.group == 2) {
                                html += "  <span class='badge badge-light-success'>企业版免费</span> ";
                            }
                            return html;
                        }
                    },
                    {
                        field: 'status', title: '状态', formatter: function (val, item) {
                            return status[item.status] + (item.status == 2 ? '<span class="badge badge-light">'+item.error_reason+'</span>' : "");
                        }
                    },
                    {
                        field: 'operate',
                        title: '',
                        formatter: function (val, item) {
                            let html = "";

                            if (item.status != 2){
                                html += "<span class='badge badge-light-success setPrice' style='cursor: pointer;'><i class=\"fas fa-yen-sign text-success\"></i></span>";
                            }

                            if (item.status == 0) {
                                html += " <span class='badge badge-light-primary createKit' style='cursor: pointer;'><i class='fas fa-upload text-primary'></i> 上传安装包</span> ";
                            }

                            if (item.status == 1) {
                                html += " <span class='badge badge-light-primary updatePlugin' style='cursor: pointer;'><i class=\"fas fa-sync-alt text-primary\"></i> 更新插件</span> ";
                            }

                            html += " <span class='badge badge-light-danger deletePlugin' style='cursor: pointer;'><i class='far fa-trash-alt text-danger'></i> 删除</span> ";

                            return html;
                        },
                        events: {
                            'click .createKit': function (event, value, row, index) {
                                createKit(row);
                            },
                            'click .updatePlugin': function (event, value, row, index) {
                                updatePlugin(row);
                            },
                            'click .setPrice': function (event, value, row, index) {
                                pluginPriceSet(row);
                            },
                            'click .deletePlugin': function (event, value, row, index) {
                                layer.confirm('你确认要从应用商店中删除 <span style="color: red;"> ' + row.plugin_name + " </span>吗，这将永久性删除该插件，并且该插件的所有用户都将无法继续使用。", {
                                    btn: ['我想好了，立即删除', '点错了，取消'],
                                    title: '<i class="far fa-trash-alt"></i> 插件退市'
                                }, function () {
                                    let layIndex = layer.load(1, {shade: [0.3, '#fff']});
                                    $.post('/admin/api/app/developerDeletePlugin', {
                                        id: row.id
                                    }, res => {
                                        layer.close(layIndex);
                                        layer.msg(res.msg);
                                        if (res.code == 200) {
                                            table.bootstrapTable('refresh', {silent: true});
                                        }
                                    });
                                });
                            },
                        }
                    }
                ]
            });


            $('.developerCreatePlugin').click(() => {
                modal();
            });


        });
    });
</script>

#{include file="../Footer.html"}